Thread: Reading in a Char*[] from file

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Reading in a Char*[] from file

    Hey. I"m trying to read a file of words into the array char* array[80] and I can't seem to make it work. I keep getting runtime errors when I use this:

    Code:
    char* word[80];
    char str[80];
    
    while(b_file.eof()==0)
    	{
    		b_file>>str;
    
    		len = strlen(str);
    		word[i] = new (char[len]);
    		
    		word[i++] =str;
    }

    What am I doing wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > word[i] = new (char[len]);
    You forgot to count the \0
    so its
    word[i] = new (char[len+1]);

    > word[i++] =str;
    Use strcpy
    strcpy( word[i++], str );

    > while(b_file.eof()==0)
    Make sure you don't go too far
    while( i < 80 && b_file.eof()==0)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM